package com.woniu.controller;


import com.woniu.entity.PageData;
import com.woniu.entity.Result;
import com.woniu.entity.dto.LoginAdminDTO;
import com.woniu.entity.dto.QueryAdminDTO;
import com.woniu.pojo.Admin;
import com.woniu.service.IAdminService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @Title
 * @Author 武术有栖
 * @Description:
 */
@RestController
@RequestMapping("/admin")
@Slf4j
public class AdminController extends BaseController {

    @Autowired
    private IAdminService adminService;
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("/login")
    public Result login(@RequestBody LoginAdminDTO loginAdminDTO){
        log.debug("进入的了登录的方法，用户名为{},密码为{}", loginAdminDTO.getUsername(), loginAdminDTO.getPassword());
        Admin admin = adminService.adminIsExist(loginAdminDTO.getUsername(), loginAdminDTO.getPassword());
        if(admin ==null){
            return toResult(false);
        }
        String uuid = UUID.randomUUID().toString();
        admin.setToken(uuid);
        adminService.saveToken(admin);
        Map<String, String> map = new HashMap<>();
        map.put("token", uuid);
        return toDataResult(map);
    }
    @GetMapping("/info")
    public Result info (String token, HttpSession session){
        log.debug("进入了info的方法，token为{}",token);
        session.setAttribute("token",token);
        Admin admin = adminService.getByToken(token);
        Map<String,Object> map = new HashMap<>();
        List<String> roles = new ArrayList<>();
        roles.add("管理员");
        map.put("name",admin.getName());
        map.put("username",admin.getUsername());
        map.put("password",admin.getPassword());
        map.put("avatar",admin.getAvatar());
        map.put("createTime",admin.getCreateTime());
        map.put("updateTime",admin.getUpdateTime());
        map.put("roles",roles);
        return toDataResult(map);
    }

    @GetMapping("/logout")
    public Result logout(String token){
        log.debug("进入的了logout的方法，token为{}", token);
        return success();
    }


    @GetMapping("/all")
    @ApiOperation("查询所有")
    public Result findAllTrainer() {
        List<Admin> adminList = adminService.list();
        return toDataResult(adminList);
    }

    @PostMapping("/search/{currentPage}/{pageSize}")
    @ApiOperation("搜索+分页")
    public Result selectTrainer(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody QueryAdminDTO adminDTO) {
        PageData pageData = adminService.select(currentPage, pageSize, adminDTO);
        return toDataResult(pageData);
    }

    @PostMapping("/add")
    @ApiOperation("添加")
    public Result addTrainer(@RequestBody Admin admin) {
        boolean add = adminService.save(admin);
        return toResult(add);
    }

    @PutMapping("/update")
    @ApiOperation("更新")
    public Result updateTrainer(@RequestBody Admin admin) {
        boolean update = adminService.updateById(admin);
        return toResult(update);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除")
    public Result deleteTrainer(@RequestBody List<String> ids) {
        boolean delete = adminService.removeBatchByIds(ids);
        return toResult(delete);
    }

    @GetMapping("/search/{id}")
    @ApiOperation("搜索+ID")
    public Result selectTrainerById(
            @PathVariable String id) {
        Admin admin = adminService.getById(id);
        return toDataResult(admin);
    }

    @GetMapping("/searchByName/{name}")
    @ApiOperation("搜索+Name")
    public Result selectTrainerByName(
            @PathVariable String name) {
        Boolean flag = adminService.getByName(name);
        return toDataResult(flag);
    }

}
